<?php
class Warehouse_HouseController extends Zend_Controller_Action
{
	/**
	* 房间列表   
	*
	*/
	private $_s;
	private $_w; //仓库
	private $_a; //仓库分区	
	private $_shelf; //货架	
	public function preDispatch(){
		$this->_s=new Warehouse_Service_House();
		$this->_w=new Warehouse_Service_Warehouse();
		$this->_a=new Warehouse_Service_Area();		
		$this->_shelf=new Warehouse_Service_Shelf();				
	}	
	
	public function indexAction(){
			
		echo Cff::renderTpl('warehouse/house/index.tpl',"layout");
	
	}	

	public function listAction(){
	
		$intpage=$this->_request->getParam('page',1);
		$pageSize=$this->_request->getParam('pageSize');
		$wa_name=$this->_request->getParam('sName','');
		$intpage = ($intpage<1)?1:$intpage; 
		$condition=array(
			'wh_code'=>$wa_name,
		);
		$count=$this->_s->getByCondition($condition,'count(*)','',0,0);
		$data=array(
			'count'=>0,
			'ask'=>0
			);
		if($count>0){
			$Rows=$this->_s->getByCondition($condition,'*','',$pageSize,$intpage);	
			foreach($Rows as $key => $value){
				if($value['warehouse_id']!=''){
					$warehouse=$this->_w->getById($value['warehouse_id']);//仓库表
					if(!empty($warehouse)){
						$Rows[$key]['warehouse_name']=$warehouse['warehouse_name'];
					}
				}
			
			}
			$data=array(
				'count'=>$count,
				'result'=>$Rows,
				'ask'=>1
			);
		}
		die(Zend_Json::encode($data));

	}	
	
	
	public function createAction(){
		$wh_code=$this->_request->getPost('wh_code');
		$wh_valuable=$this->_request->getPost('wh_valuable');			
		$wa_code=$this->_request->getPost('wa_code','');
		$warehouse_id=$this->_request->getPost('warehouse_id','');
		$wh_status=$this->_request->getPost('wh_status','');
		$wh_note=$this->_request->getPost('wh_note','');
		$wh_id=$this->_request->getPost('wh_id','');
		$error=array();
		$date=date("Y-m-d H:i:s");
		$message['ask']=0;
		$data=0;
		$rows=0;
		$row=array(
			'wh_code'=>$wh_code,
			'wh_valuable'=>$wh_valuable,
			'wa_code'=>$wa_code,
			'warehouse_id'=>$warehouse_id,
			'wh_status'=>$wh_status,
			'wh_note'=>$wh_note,
		);
		$error = $this->_s->validator($row);//表单数据验证
        if(!empty($error)){
			$return=array(
				'ask'=>0,
				'error'=>$error,
			);
            die(Zend_Json::encode($return));
        }
		$Arows=$this->_a->getByfield('wa_code',$row['wa_code']);//分区是否存在
		if(!empty($wh_id)){//修改
			$inforows=$this->_s->getById($wh_id);
			if(!empty($inforows)){		
				if($inforows['wa_code']==$wa_code && $inforows['wh_valuable']==$wh_valuable && $inforows['warehouse_id']==$warehouse_id && $inforows['wh_note']==$wh_note && $inforows['wh_status']==$wh_status){
					$data=3; //没有任何修改	
				}elseif(empty($Arows)){
					$data=5;    //分区不存在
				}else{
					unset($row['wh_code']); //不允许修改房间号
					$rows=$this->_s->update($row,$wh_id);
					if($inforows['wh_status']!=$wh_status){
						$Row=$this->_shelf->updateStatus($inforows['wa_code'],$wh_status);//级联修改shelf
					}
					$data=2; //修改成功
				}
			}
		}else{ 	//添加
			$row['wh_create_date']=$date;
			$Srows=$this->_s->getByfield('wh_code',$row['wh_code']);//房间号唯一
			if(!empty($Srows)){
				$data=4;    //已经存在
			}elseif(empty($Arows)){
				$data=5;    //分区不存在
			}else{
				$rows=$this->_s->create($row);
				if($rows>0){
					$data=1; //添加成功
				}
			}
			
		}
		$message['ask']=$data;
		die(Zend_Json::encode($message));
	}


	public function infoAction(){
		$wh_id=$this->_request->getParam('wh_id');
		$return=array(
			'ask'=>0
		);
		if(!empty($wh_id)){
			$rows=$this->_s->getById($wh_id);
			$return=array(
				'ask'=>1,
				'result'=>$rows
			);			
		}
		die(Zend_Json::encode($return));
	}	

	public function deleteAction(){
		$t_id=$this->_request->getPost('wh_id');
		$rows=0;
        try{
			if(!empty($t_id)){
				$rows=$this->_s->delete($t_id);
			}
			if($rows>0){
				$massage['ask']=1;
			}else{
				$massage['ask']=0;
			}
        }catch(Zend_Exception $e){
			$massage['ask']=0;
			$massage['result']=$e->getMessage();
        }		
		die(Zend_Json::encode($massage));
	}	
	
	public function selectAction(){
		$data=array('ask'=>0);
		$result=$this->_w->getByCondition('','*','',0,0);  //仓库
		if(!empty($result)){
			$data=array(
				'ask'=>1,
				'result'=>$result,
			);
		}
		die(Zend_Json::encode($data));
	}

	/**
	*房间列表
	*shelf 等页面引用加载 
	*@return json
	*/	
	public function selectlistAction(){
		$data=array('ask'=>0);
		$result=$this->_s->getByCondition('','*','wh_code',0,0);  
		if(!empty($result)){
			$data=array(
				'ask'=>1,
				'result'=>$result,
			);
		}
		die(Zend_Json::encode($data));
	}		
	
	
}